package com.younion.util;

import java.text.DateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//验证? create by seagull 2008-05-02
/*--------------------------------------------------|
 |validator 9.66                                     |
 |create by GaoZuhui(winner) 2008-04-15              |
 |description: public JavaScript Validate functions  |
 |--------------------------------------------------*/

///************************************************
//function list
//1.isEmpty(elementValue)
//2.isSelected(elementValue)
//3.isChinese(elementValue)
//4.isEnglish(elementValue)
//5.isNumber(elementValue)
//6.isInteger(elementValue)
//7.isDouble(elementValue)
//8.isEmail(elementValue)
//9.isUrl(elementValue)
//10.isPhoneNo(elementValue)
//11.isCurrency(elementValue)
//12.isMobileNo(elementValue)
//13.isBeginMobileNo(elementValue)
//14.isEndMobileNo(elementValue)
//15.isPostNo(elementValue)
//16.isIdCard(elementValue)
//17.isQQNo(elementValue)
//18.isDate(elementValue, formatString)
//19.isSafe(elementValue)
//20.compare(op1,operator,op2)
//21.mustChecked(name, min, max)
//22.isMatch(op, reg)
//23.isLimit(len,minLength, maxLength)
//24.isBetween(elementValue,minValue, maxValue)
//25.lengthB(str)
//26.trim(str)
//27.cutStrLength(str, iLen) 
//28.checkFieldLength(fieldName, fieldDesc, fieldLength)
public class Validator {
	/**
	 * create by seagull 2008-04-18 验证是否为合法的移动手机号码格式
	 * 
	 * @param card_number卡件号码
	 * @return 非法卡号
	 */

	/**
	 * 正则表达式验?
	 */
	public final static boolean isMatch(String reg, String elementValue) {

		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile(reg); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为空
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isEmpty(String elementValue) {
		boolean bool = true;
		bool = ("".equals(elementValue) || (null == elementValue) || (elementValue
				.length() == 0));
		return bool;
	}

	/**
	 * 验证下拉列表框是否有选择?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isSelected(String elementValue) {
		return (!"".equals(elementValue) && !(elementValue.length() == 0) && !(elementValue == "-1"));
	}

	/**
	 * 验证是否为中文字?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isChinese(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("[\u0391-\uFFE5]+"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为英文字?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isEnglish(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("[A-Za-z]+"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为数?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isNumber(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("\\d+"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为整?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isInteger(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("[-\\+]?\\d+"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为整?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isDouble(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("[-\\+]?\\d+(\\.\\d+)?"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为邮件地?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isEmail(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern
				.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为网?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isUrl(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern
				.compile("http:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\]\\':+!]*([^<>\"\"])*"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为电话号?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isPhoneNo(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern
				.compile("((\\(\\d{2,3}\\))|(\\d{3}\\-))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\-\\d{1,4})?"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为货?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isCurrency(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("\\d+(\\.\\d+)?"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为移动手机号?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isMobileNo(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("((\\+86)|(86))?((13)[4-9]|(15)[0-9])\\d{8}"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为品牌卡类型
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isCardType(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("([1-6])"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证品牌卡的使用类型
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isCardUseType(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("([0-5])"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证品牌卡的分盒类型
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isCardGroupType(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("([LS])"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为邮政编?
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isPostNo(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("[1-9]\\d{5}"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 验证是否为身份证号码
	 * 
	 * @param elementValue
	 * @return
	 */
	public final static boolean isIdCard(String elementValue) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("((\\d{15})|(\\d{18})|(\\d{17}[xX]))"); // 正则表达?
		m = p.matcher(elementValue);
		bool = m.matches();

		return bool;
	}

	/**
	 * 判断文件是否为正确的格式
	 */
	public final static boolean validateFileType(String fileName,
			String fileType) {
		boolean bool = true;
		if ("".equals(fileName) || null == fileName || "".equals(fileType)
				|| null == fileType) {
			bool = false;
			return bool;
		}
		bool = fileName.substring(fileName.length() - fileType.length())
				.equalsIgnoreCase(fileType);
		return bool;
	}

	/**
	 * create by seagull 2008-05-06 判断散号资源是否为合法的移动手机号码格式
	 * 
	 * @param cardnumber_list_arr
	 *            卡号资源数组格式参数
	 * @param resource_type
	 *            资源类型
	 * @return 非法卡号
	 */
	public final static String validateCardNumbers(String[] cardnumber_list_arr) {
		String result = "";
		for (int i = 0; i < cardnumber_list_arr.length; i++) {
			if (!Validator.isMobileNo(cardnumber_list_arr[i])) {
				result += cardnumber_list_arr[i] + "\\n";
			}
		}
		return result;
	}

	/**
	 * 验证新业务消息格?(客户手机号码#业务??,客户手机号码#拼音??,客户手机号码#中文名称)
	 * 
	 * @param newBusinessMessage
	 * @return
	 */
	public final static boolean validateNewBusinessMessageFormat(
			String newBusinessMessage) {
		boolean bool = true;
		Pattern p = null;
		Matcher m = null;

		p = Pattern.compile("((13)[4-9]|(15)[0-9])\\d{8}([#＃])(.+)"); // 正则表达?
		m = p.matcher(newBusinessMessage);
		bool = m.matches();

		return bool;
	}

	/**
	 * 判断某字节是否为数字
	 * 
	 * @param ch
	 *            char
	 * @return boolean:true:是数?;false:不是数字;
	 */
	public final static boolean isDigit(char ch) {
		Pattern pattern = Pattern.compile("[0-9]");
		Matcher matcher = pattern.matcher(String.valueOf(ch));
		boolean flag = matcher.matches();
		return flag;
	}

	/**
	 * 判断某字节是否为字母
	 * 
	 * @param ch
	 *            char
	 * @return boolean:true:是字?;false:不是字母
	 */
	public final static boolean isLetter(char ch) {
		Pattern pattern = Pattern.compile("[A-Za-z]");
		Matcher matcher = pattern.matcher(String.valueOf(ch));
		boolean flag = matcher.matches();
		return flag;
	}

	/**
	 * 判断字符串是否为全数?
	 * 
	 * @param str
	 *            String
	 * @return boolean:true:是数?;false:不是数字;
	 */
	public final static boolean isDigit(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher matcher = pattern.matcher(str);
		boolean flag = matcher.matches();
		return flag;
	}

	/**
	 * 判断字符串是否为全字?
	 * 
	 * @param ch
	 *            char
	 * @return boolean:true:是字?;false:不是字母
	 */
	public final static boolean isLetter(String str) {
		Pattern pattern = Pattern.compile("[A-Za-z]*");
		Matcher matcher = pattern.matcher(str);
		boolean flag = matcher.matches();
		return flag;
	}

	/**
	 * 判断是否浮点型数?
	 */
	public final static boolean checkFloat(String str) {
		boolean b = true;
		int flag = 0;
		String model = "-1234567890.";
		char strs[] = str.toCharArray();
		for (int i = 0; i < strs.length; i++) {
			flag = model.indexOf(String.valueOf(strs[i]));
			if (flag == -1) {
				b = false;// 不存在这样的字符，??出，不是浮点?
				return b;
			}
		}
		return b;
	}

	// ?查日?,合法返回?
	public final static boolean checkDate(String date) {
		if (date == null || date == "")
			return false;
		DateFormat fmt = DateFormat.getDateInstance(DateFormat.DEFAULT);
		try {
			fmt.parse(date.trim());
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public final static boolean isValidDate(String sDate) {
		boolean b = true;
		String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
		String datePattern2 = "\\d{2}/\\d{2}/\\d{4}";

		if ((sDate != null)) {
			Pattern pattern = Pattern.compile(datePattern1);
			Matcher match = pattern.matcher(sDate);
			b = match.matches();
			if (!b) {
				pattern = Pattern.compile(datePattern2);
				match = pattern.matcher(sDate);
				b = match.matches();
			}
		}
		return b;
	}

}
